//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using CuoiHoi.Entities;
using CuoiHoi.DataAccess;

namespace CuoiHoi.Business
{
    public class BTheLoai
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TheLoai_SelectAll", null);
            return all;
        }
         public static DataTable   SelectByidTheLoaiCha(string idTheLoaiCha)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idTheLoaiCha", idTheLoaiCha);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TheLoai_SelectByidTheLoaiCha", pr);
            return all;
        }
 
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TheLoai_SelectTop", pr);
            return all;
        }
        public static DataTable SelectThuTu(int idTheLoai)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idTheLoai", idTheLoai);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TheLoai_SelectThuTu", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETheLoai OTheLoai)
        {
            SqlParameter[] pr = new SqlParameter[8];
            pr[0] = new SqlParameter(@"TenTheLoai", OTheLoai.TenTheLoai);
            pr[1] = new SqlParameter(@"DuongDan", OTheLoai.DuongDan);
            pr[2] = new SqlParameter(@"ThuTu", OTheLoai.ThuTu);
            pr[3] = new SqlParameter(@"idTheLoaiCha", OTheLoai.idTheLoaiCha);
            pr[4] = new SqlParameter(@"Title", OTheLoai.Title);
            pr[5] = new SqlParameter(@"Description", OTheLoai.Description);
            pr[6] = new SqlParameter(@"KeyWord", OTheLoai.Keyword);
            pr[7] = new SqlParameter(@"TrangThai", OTheLoai.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TheLoai_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETheLoai OTheLoai)
        {
            SqlParameter[] pr = new SqlParameter[8];
            pr[0] = new SqlParameter(@"idTheLoai", OTheLoai.idTheLoai);
            pr[1] = new SqlParameter(@"TenTheLoai", OTheLoai.TenTheLoai);
            pr[2] = new SqlParameter(@"DuongDan", OTheLoai.DuongDan);
            //pr[3] = new SqlParameter(@"ThuTu", OTheLoai.ThuTu);
            pr[3] = new SqlParameter(@"idTheLoaiCha", OTheLoai.idTheLoaiCha);
            pr[4] = new SqlParameter(@"Title", OTheLoai.Title);
            pr[5] = new SqlParameter(@"Description", OTheLoai.Description);
            pr[6] = new SqlParameter(@"KeyWord", OTheLoai.Keyword);
            pr[7] = new SqlParameter(@"TrangThai", OTheLoai.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TheLoai_Update", pr);
        }
        public static void UpdateThuTu(ETheLoai OTheLoai)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"idTheLoai", OTheLoai.idTheLoai);
            pr[1] = new SqlParameter(@"ThuTuMoi", OTheLoai.ThuTu);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TheLoai_UpdateThuTu", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int idTheLoai)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idTheLoai",idTheLoai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TheLoai_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETheLoai SelectByID(int idTheLoai)
        {
            ETheLoai OTheLoai = new ETheLoai();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"idTheLoai",idTheLoai);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TheLoai_SelectByID", pr);
            if (idr.Read())
              OTheLoai = GetOneTheLoai(idr);
            idr.Close();
            idr.Dispose();
            return OTheLoai;
        }
        public static ETheLoai SelectByDuongDan(string TenDuongDan)
        {
            ETheLoai OTheLoai = new ETheLoai();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenDuongDan", TenDuongDan);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TheLoai_SelectByTenDuongDan", pr);
            if (idr.Read())
                OTheLoai = GetOneTheLoai(idr);
            idr.Close();
            idr.Dispose();
            return OTheLoai;
        }
//-----------------------------------------------------------//
        private static ETheLoai GetOneTheLoai(IDataReader idr)
       {
            ETheLoai OTheLoai = new ETheLoai();
                if (idr["idTheLoai"] != DBNull.Value)
                    OTheLoai.idTheLoai = (int)idr["idTheLoai"];
                if (idr["TenTheLoai"] != DBNull.Value)
                    OTheLoai.TenTheLoai = (string)idr["TenTheLoai"];
                if (idr["DuongDan"] != DBNull.Value)
                    OTheLoai.DuongDan = (string)idr["DuongDan"];
                if (idr["ThuTu"] != DBNull.Value)
                    OTheLoai.ThuTu = (byte)idr["ThuTu"];
                if (idr["idTheLoaiCha"] != DBNull.Value)
                    OTheLoai.idTheLoaiCha = (string)idr["idTheLoaiCha"];
                if (idr["Title"] != DBNull.Value)
                    OTheLoai.Title = (string)idr["Title"];
                if (idr["Description"] != DBNull.Value)
                    OTheLoai.Description = (string)idr["Description"];
                if (idr["Keyword"] != DBNull.Value)
                    OTheLoai.Keyword = (string)idr["Keyword"];
            return OTheLoai;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int idTheLoai)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"idTheLoai",idTheLoai);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TheLoai_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETheLoai> ListAll()
        {
            List<ETheLoai> list = new List<ETheLoai>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TheLoai_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTheLoai(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETheLoai> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETheLoai> list = new List<ETheLoai>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TheLoai_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTheLoai(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}